Ext.ns('Ext.ux.layout');
Ext.ux.layout.CenterLayout = Ext.extend(Ext.layout.FitLayout, {
			// private
			setItemSize : function(item, size) {
				this.container.addClass('ux-layout-center');
				item.addClass('ux-layout-center-item');
				if (item && size.height > 0) {
					if (item.width) {
						size.width = item.width;
					}
					item.setSize(size);
				}
			}
		});

Ext.Container.LAYOUTS['ux.center'] = Ext.ux.layout.CenterLayout;

Ext.ux.TabCloseMenu = function() {
	var tabs, menu, ctxItem;
	this.init = function(tp) {
		tabs = tp;
		tabs.on('contextmenu', onContextMenu);
	}
	function onContextMenu(ts, item, e) {
		if (!menu) {
			menu = new Ext.menu.Menu([{
						id : tabs.id + '-close',
						text : 'Zakmnij kartę',
						handler : function() {
							tabs.remove(ctxItem);
						}
					}, {
						id : tabs.id + '-close-others',
						text : 'Zamknij inne karty',
						handler : function() {
							tabs.items.each(function(item) {
										if (item.closable && item != ctxItem) {
											tabs.remove(item);
										}
									});
						}
					}]);
		}
		ctxItem = item;
		var items = menu.items;
		items.get(tabs.id + '-close').setDisabled(!item.closable);
		var disableOthers = true;
		tabs.items.each(function() {
					if (this != item && this.closable) {
						disableOthers = false;
						return false;
					}
				});
		items.get(tabs.id + '-close-others').setDisabled(disableOthers);
		menu.showAt(e.getPoint());
	}
};
Ext.ux.TabScrollerMenu = Ext.extend(Object, {
			pageSize : 10,
			maxText : 15,
			menuPrefixText : 'Obiekty',
			constructor : function(config) {
				config = config || {};
				Ext.apply(this, config);
			},
			init : function(tabPanel) {
				Ext.apply(tabPanel, this.tabPanelMethods);
				tabPanel.tabScrollerMenu = this;
				var thisRef = this;
				tabPanel.on({
							render : {
								scope : tabPanel,
								single : true,
								fn : function() {
									var newFn = tabPanel.createScrollers
											.createSequence(
													thisRef.createPanelsMenu,
													this);
									tabPanel.createScrollers = newFn;
								}
							}
						});
			},
			createPanelsMenu : function() {
				var h = this.stripWrap.dom.offsetHeight;
				var rtScrBtn = this.header.dom.firstChild;
				Ext.fly(rtScrBtn).applyStyles({
							right : '18px'
						});
				var stripWrap = Ext.get(this.strip.dom.parentNode);
				stripWrap.applyStyles({
							'margin-right' : '36px'
						});
				var scrollMenu = this.header.insertFirst({
							cls : 'x-tab-tabmenu-right'
						});
				scrollMenu.setHeight(h);
				scrollMenu.addClassOnOver('x-tab-tabmenu-over');
				scrollMenu.on('click', this.showTabsMenu, this);
				this.scrollLeft.show = this.scrollLeft.show.createSequence(
						function() {
							scrollMenu.show();
						});
				this.scrollLeft.hide = this.scrollLeft.hide.createSequence(
						function() {
							scrollMenu.hide();
						});
			},
			getPageSize : function() {
				return this.pageSize;
			},
			setPageSize : function(pageSize) {
				this.pageSize = pageSize;
			},
			getMaxText : function() {
				return this.maxText;
			},
			setMaxText : function(t) {
				this.maxText = t;
			},
			getMenuPrefixText : function() {
				return this.menuPrefixText;
			},
			setMenuPrefixText : function(t) {
				this.menuPrefixText = t;
			},
			tabPanelMethods : {
				showTabsMenu : function(e) {
					if (!this.tabsMenu) {
						this.tabsMenu = new Ext.menu.Menu();
						this.on('beforedestroy', this.tabsMenu.destroy,
								this.tabsMenu);
					}
					this.tabsMenu.removeAll();
					this.generateTabMenuItems();
					var target = Ext.get(e.getTarget());
					var xy = target.getXY();
					xy[1] += 24;
					xy[0] -= 100;
					this.tabsMenu.showAt(xy);
				},
				generateTabMenuItems : function() {
					var curActive = this.getActiveTab();
					var totalItems = this.items.getCount();
					var pageSize = this.tabScrollerMenu.getPageSize();
					if (totalItems > pageSize) {
						var numSubMenus = Math.floor(totalItems / pageSize);
						var remainder = totalItems % pageSize;
						for (var i = 0; i < numSubMenus; i++) {
							var curPage = (i + 1) * pageSize;
							var menuItems = [];
							for (var x = 0; x < pageSize; x++) {
								index = x + curPage - pageSize;
								var item = this.items.get(index);
								menuItems.push(this.autoGenMenuItem(item));
							}
							this.tabsMenu.add({
										text : this.tabScrollerMenu
												.getMenuPrefixText()
												+ ' '
												+ (curPage - pageSize + 1)
												+ ' - ' + curPage,
										menu : menuItems
									});
						}
						if (remainder > 0) {
							var start = numSubMenus * pageSize;
							menuItems = [];
							for (var i = start; i < totalItems; i++) {
								var item = this.items.get(i);
								menuItems.push(this.autoGenMenuItem(item));
							}
							this.tabsMenu.add({
										text : this.tabScrollerMenu.menuPrefixText
												+ ' '
												+ (start + 1)
												+ ' - '
												+ (start + menuItems.length),
										menu : menuItems
									});
						}
					} else {
						this.items.each(function(item) {
									if (item.id != curActive.id && !item.hidden) {
										menuItems.push(this
												.autoGenMenuItem(item));
									}
								}, this);
					}
				},
				autoGenMenuItem : function(item) {
					var maxText = this.tabScrollerMenu.getMaxText();
					var text = Ext.util.Format.ellipsis(item.title, maxText);
					return {
						text : text,
						handler : this.showTabFromMenu,
						scope : this,
						disabled : item.disabled,
						tabToShow : item,
						iconCls : item.iconCls
					}
				},
				showTabFromMenu : function(menuItem) {
					this.setActiveTab(menuItem.tabToShow);
				}
			}
		});
Ext.App = function(config) {
	this.initStateProvider();
	this.views = [];
	Ext.apply(this, config);
	if (!this.api.actions) {
		this.api.actions = {};
	}
	Ext.onReady(this.onReady, this);
	Ext.App.superclass.constructor.apply(this, arguments);
}
Ext.extend(Ext.App, Ext.util.Observable, {
	STATUS_EXCEPTION : 'exception',
	STATUS_VALIDATION_ERROR : "validation",
	STATUS_ERROR : "error",
	STATUS_NOTICE : "notice",
	STATUS_OK : "ok",
	STATUS_HELP : "help",
	api : {
		url : null,
		type : null,
		actions : {}
	},
	msgCt : null,
	onReady : function() {
		this.msgCt = Ext.DomHelper.insertFirst(document.body, {
					id : 'msg-div'
				}, true);
		this.msgCt.setStyle('position', 'absolute');
		this.msgCt.setStyle('z-index', 9999);
		this.msgCt.setWidth(300);
	},
	initStateProvider : function() {
		var days = '';
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var exptime = "; expires=" + date.toGMTString();
		} else {
			var exptime = null;
		}
		Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
					path : '/',
					expires : exptime,
					domain : null,
					secure : false
				}));
	},
	registerView : function(view) {
		this.views.push(view);
	},
	getViews : function() {
		return this.views;
	},
	registerActions : function(actions) {
		Ext.apply(this.api.actions, actions);
	},
	getAPI : function() {
		return this.api;
	},
	setAlert : function(status, msg) {
		this.addMessage(status, msg);
	},
	addMessage : function(status, msg) {
		var delay = 3;
		if (status == false) {
			delay = 5;
		}
		delay = msg.length / 13.3;
		if (delay < 3) {
			delay = 3;
		} else if (delay > 9) {
			delay = 9;
		}
		this.msgCt.alignTo(document, 't-t');
		Ext.DomHelper.append(this.msgCt, {
			html : this.buildMessageBox(status, String.format.apply(String,
							Array.prototype.slice.call(arguments, 1)))
		}, true).slideIn('t').pause(delay).ghost("t", {
					remove : true
				});
	},
	buildMessageBox : function(title, msg) {
		switch (title) {
			case true :
				title = this.STATUS_OK;
				break;
			case false :
				title = this.STATUS_ERROR;
				break;
		}
		return [
				'<div class="app-msg">',
				'<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
				'<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3 class="x-icon-text icon-status-'
						+ title + '">',
				title,
				'</h3>',
				msg,
				'</div></div></div>',
				'<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
				'</div>'].join('');
	},
	decodeStatusIcon : function(status) {
		iconCls = '';
		switch (status) {
			case true :
			case this.STATUS_OK :
				iconCls = this.ICON_OK;
				break;
			case this.STATUS_NOTICE :
				iconCls = this.ICON_NOTICE;
				break;
			case false :
			case this.STATUS_ERROR :
				iconCls = this.ICON_ERROR;
				break;
			case this.STATUS_HELP :
				iconCls = this.ICON_HELP;
				break;
		}
		return iconCls;
	},
	setViewState : function(key, value) {
		Ext.state.Manager.set(key, value);
	},
	getViewState : function(key) {
		return Ext.state.Manager.get(key);
	},
	t : function(words) {
		return words;
	},
	handleResponse : function(res) {
		if (res.type == this.STATUS_EXCEPTION) {
			return this.handleException(res);
		}
		if (res.message.length > 0) {
			this.setAlert(res.status, res.message);
		}
	},
	handleException : function(res) {
		Ext.MessageBox.alert(res.type.toUpperCase(), res.message);
	}
});
Ext.ns('Ext.ux.grid');
/*
 * </code></pre> In addition to storing a Boolean value within the record data,
 * this class toggles a css class between <tt>'x-grid3-check-col'</tt> and
 * <tt>'x-grid3-check-col-on'</tt> to alter the background image used for a
 * column.
 */
Ext.ux.grid.CheckColumn = function(config) {
	Ext.apply(this, config);
	if (!this.id) {
		this.id = Ext.id();
	}
	this.renderer = this.renderer.createDelegate(this);
};
Ext.ux.grid.CheckColumn.prototype = {
	init : function(grid) {
		this.grid = grid;
		this.grid.on('render', function() {
					var view = this.grid.getView();
					view.mainBody.on('mousedown', this.onMouseDown, this);
				}, this);
	},
	onMouseDown : function(e, t) {
		if (t.className && t.className.indexOf('x-grid3-cc-' + this.id) != -1) {
			e.stopEvent();
			var index = this.grid.getView().findRowIndex(t);
			var record = this.grid.store.getAt(index);
			record.set(this.dataIndex, !record.data[this.dataIndex]);
		}
	},
	renderer : function(v, p, record) {
		p.css += ' x-grid3-check-col-td';
		return '<div class="x-grid3-check-col' + (v ? '-on' : '')
				+ ' x-grid3-cc-' + this.id + '">&#160;</div>';
	}
};
Ext.preg('checkcolumn', Ext.ux.grid.CheckColumn);
Ext.grid.CheckColumn = Ext.ux.grid.CheckColumn;

combo = function(conf) {
	var config = conf || {
		editable : true
		,
	};
	var com = new Ext.form.ComboBox({
				editable : config.editable,
				name : 'schema',
				fieldLabel : 'Schemat',
				displayField : 'name',
				triggerAction : 'all',
				minChars : 2,
				forceSelection : true,
				enableKeyEvents : true,
				resizable : true,
				minListWidth : 220,
				allowBlank : true,
				width : 120,
				loadingText : 'Wczytywanie schem...',
				store : new Ext.data.JsonStore({
							id : 'name',
							root : 'data',
							totalProperty : 'totalCount',
							fields : [{
										name : 'name',
										type : 'string'
									}],
							url : 'login/schemas.jsp',
							baseParams : {
								cmd : 'getSchemas'
							}
						}),
				listeners : {
					select : function(combo, record, index) {
						this.setRawValue(record.get('name'));
					},
					blur : function() {
						var val = this.getRawValue();
					},
					render : function() {
						this.validate();
					},
					keypress : {
						buffer : 100,
						fn : function() {
							if (!this.getRawValue()) {
								this.doQuery('', true);
							}
						}
					}
				}
			});
	Ext.apply(com, config);
	return com;
};
comboRO = function(config) {
	var com = combo({
				enableKeyEvents : true,
				forceSelection : true,
				readOnly : true,
				editable : false,
				listeners : {}
			});
	Ext.apply(com, config);
	return com;
};
comboTS = function(config) {
	var store = new Ext.data.JsonStore({
				id : 'name',
				root : 'data',
				totalProperty : 'totalCount',
				fields : [{
							name : 'name',
							type : 'string'
						}],
				url : 'db/tablespace.jsp'
			});
	var com = combo({
				editable : false,
				name : 'tablespace',
				fieldLabel : 'Przestrzeń tabel',
				loadingText : 'Wczytywanie przestrzeni tabel...',
				store : store
			});
	Ext.apply(com, config);
	return com;
};
var role_store = new Ext.data.JsonStore({
			id : 'role_store',
			root : 'data',
			totalProperty : 'totalCount',
			sortInfo: {field:'name', direction:'ASC'},
			fields : [{
						name : 'name'
					}],
			url : 'db/roles.jsp'
		});
var sys_privs_store = new Ext.data.JsonStore({
			id : 'sys_privs_store',
			root : 'data',
			totalProperty : 'totalCount',
			sortInfo: {field:'name', direction:'ASC'},
			fields : [{
						name : 'name'
					}],
			url : 'db/sys_privs.jsp'
		});
comboPROFILE = function(config) {
	var store = new Ext.data.JsonStore({
				id : 'name',
				root : 'data',
				totalProperty : 'totalCount',
				fields : [{
							name : 'name'
						}],
				sortInfo: {
            		field: 'name',
            		direction: 'ASC'
        		},
				url : 'db/profile.jsp'
			});
	var com = combo({
				editable : false,
				name : 'profile',
				fieldLabel : 'Profil',
				loadingText : 'Wczytywanie profili...',
				store : store
			});
	Ext.apply(com, config);
	return com;
};
ta = function(config) {
	var he = new Ext.form.TextArea({
				height : 210,
				name : 'source',
				allowBlank : false
			});
	Ext.apply(he, config);
	return he;
};
SqlEditor = Ext.extend(Ext.form.Field, {
			readOnly : false,
			initialized : false,
			height : 210,
			name : 'source',
			border : false,
			allowBlank : false,
			initComponent : function() {
				SqlEditor.superclass.initComponent.call(this);
				this.addEvents({
							initialize : true,
							activate : true
						});
			},
			focus : function() {
			},
			adjustSize : Ext.BoxComponent.prototype.adjustSize,
			blur : function() {
				this.value = editAreaLoader.getValue(this.id);
			},
			onDestroy : function() {
				if (this.rendered) {
					this.wrap.dom.innerHTML = '';
					this.wrap.remove();
				}
			},
			onRender : function(ct, position) {
				SqlEditor.superclass.onRender.call(this, ct, position);
				this.el.dom.style.border = '0 none';
				this.el.dom.setAttribute('tabIndex', -1);
				this.el.addClass('x-hidden');
				if (Ext.isIE) {
					this.el.applyStyles('margin-top:-1px;margin-bottom:-1px;')
				}
				this.wrap = this.el.wrap({
							height : '100%'
						});
				this.textarea = Ext.get(document.createElement('textarea'));
				this.textarea.dom.disabled = false;
				this.textarea.dom.style.overflow = 'auto';
				this.textarea.dom.style.height = '350px';
				this.textarea.dom.style.width = '100%';
				this.options = this.textarea.dom.className;
				this.wrap.dom.appendChild(this.textarea.dom);
				this.id = this.textarea.id;
				editAreaLoader.init({
							allow_toggle : true,
							id : this.textarea.id,
							syntax : "oracle",
							start_highlight : true,
							show_line_colors : true,
							allow_resize : "no",
							toolbar : '',
							autocompletion_start : true
						});
				editAreaLoader.setValue(this.id, this.initialText);
			},
			initialize : function() {
				this.initialized = true;
				this.fireEvent('initialize', this);
			},
			getValue : function() {
				return editAreaLoader.getValue(this.id);
			}
			,
		});
dll = function(n) {
	Ext.Ajax.request({
				url : 'output/ddl.jsp',
				method : 'POST',

				success : function(result, request) {
					new Ext.Window({
								id : "ddl",
								title : "DDL",
								height : 430,
								width : 600,
								autoShow : true,
								collapsible : false,
								constrain : true,
								plain : true,
								modal : true,
								maximizable : true,
								layout : 'fit',
								keys : [{
											key : 27,
											scope : this,
											fn : function() {
												this.close();
												if (ddl.cancelEdit)
													ddl.cancelEdit();
											}
										}],
								items : new SqlEditor({
											initialText : result.responseText,
											readOnly : true
										})
							}).show();
				},
				failure : function(result, request) {
					alert('blad');
				},
				params : {
					type : n.attributes.t,
					name : n.text,
					schema : n.attributes.schema
				}
			});
}
Msg=Ext.Msg.alert; 
